
clear

set matsize 2000

*** GENERATE LAGGED VOTE VARIABLE DATASET ***

use TimelinePartiesMI_New.dta, clear

drop daysbeforeED_0_001
drop t_*
rename tk_* t_*

* TRANSFORM THE TIMELINE VARIABLE TO NEW FUNCTIONAL FORM
nl (altlog_ae = {b0} + {b1}*daysbeforeED + {b2}*daysbeforeED^.0001), initial(b0 1 b1 1 b2 1)
predict daysbeforeED_0_001
sum daysbeforeED_0_001
replace daysbeforeED_0_001=daysbeforeED_0_001+0.3512451
sum daysbeforeED_0_001
replace daysbeforeED_0_001=daysbeforeED_0_001/1.005601

* INTERACTIONS WITH ALTERNATIVE TIMELINE VARIABLE
gen tk_pr=daysbeforeED_0_001*pr
gen tk_enpp=daysbeforeED_0_001*enpp
gen tk_partycentric=daysbeforeED_0_001*partycentric
gen tk_vote=daysbeforeED_0_001*vote_
gen tk_partyold=daysbeforeED_0_001*partyold
gen tk_niche=daysbeforeED_0_001*niche
gen tk_gov=daysbeforeED_0_001*gov_
gen tk_inc=daysbeforeED_0_001*inc_

gen tk_NielsonBin=daysbeforeED_0_001*NielsonBin
gen tk_NielsonRnk=daysbeforeED_0_001*NielsonRnk

gen tk_enpp_a=daysbeforeED_0_001*enpp_a
gen tk_enpp_b=daysbeforeED_0_001*enpp_b

**************************************************************************************************************************************************************************************
** SET UP MULTIPLE IMPUTATION ********************************************************************************************************************************************************
**************************************************************************************************************************************************************************************

foreach x of numlist 1/50  {
replace _`x'_iae=0.000001 if _`x'_iae==0
}

foreach x of numlist 1/50 { 
gen _`x'_altlog_iae=altlog_ae 
replace _`x'_altlog_iae =ln(_`x'_iae) if missing(altlog_ae)
}

drop ae__*_

mi unset

save TimelinePartiesMI_Alt.dta, replace

use TimelinePartiesMI_Alt.dta, clear

mi import wide, imputed( altlog_ae = _1_altlog_iae _2_altlog_iae _3_altlog_iae _4_altlog_iae _5_altlog_iae _6_altlog_iae _7_altlog_iae _8_altlog_iae _9_altlog_iae _10_altlog_iae _11_altlog_iae _12_altlog_iae _13_altlog_iae _14_altlog_iae _15_altlog_iae _16_altlog_iae _17_altlog_iae _18_altlog_iae _19_altlog_iae _20_altlog_iae _21_altlog_iae _22_altlog_iae _23_altlog_iae _24_altlog_iae _25_altlog_iae _26_altlog_iae _27_altlog_iae _28_altlog_iae _29_altlog_iae _30_altlog_iae _31_altlog_iae _32_altlog_iae _33_altlog_iae _34_altlog_iae _35_altlog_iae _36_altlog_iae _37_altlog_iae _38_altlog_iae _39_altlog_iae _40_altlog_iae _41_altlog_iae _42_altlog_iae _43_altlog_iae _44_altlog_iae _45_altlog_iae _46_altlog_iae _47_altlog_iae _48_altlog_iae _49_altlog_iae _50_altlog_iae )

mi register regular vote_
mi xtset, clear 
mi set M = 50

save TimelinePartiesMI_Alt.dta, replace

**************************************************************************************************************************************************************************************
** TABLE 1 - MAIN ANALYSIS ***********************************************************************************************************************************************************
**************************************************************************************************************************************************************************************

use TimelinePartiesMI_Alt.dta, clear

** TABLE 1 **

* TIMELINE 
reg altlog_ae daysbeforeED_0_001   
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(1)") title("", "Legislative elections") replace

* TIMELINE + ERROR 
reg altlog_ae daysbeforeED_0_001 ipollse
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(2)") title("", "Legislative elections") merge

* TIMELINE + ERROR + PR
reg altlog_ae daysbeforeED_0_001 ipollse pr tk_pr
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(3)") title("", "Legislative elections") merge

* TIMELINE + ERROR + PR + ENP + PARTY SIZE
reg altlog_ae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(4)") title("", "Legislative elections") merge

* TIMELINE + ERROR + ENP + PARTY SIZE + PARTY TRAITS
reg altlog_ae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc 
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(5)") title("", "Legislative elections") merge

* INTERPOLATED 
reg altlog_iae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc
*outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(7, Interpolated)") title("", "Legislative elections") merge

* INTERPOLATED (PARTY CONTROLS)
reg altlog_iae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc, absorb(_couXpar_)
*outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**) starlevels(1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(8*, Interpolated, party controls)") title("", "Legislative elections") merge

* MULTIPLE IMPUTATION 
mi estimate, post dots: reg altlog_ae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**,***) starlevels(5 1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(6, Multiply imputed)") title("", "Legislative elections") merge
mibeta altlog_ae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc

* MULTIPLE IMPUTATION (PARTY CONTROLS)
mi estimate, post dots: reg altlog_ae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc, absorb(_couXpar_)
outreg using EJPR_TabS3, se bdec(3) sigsymb(*,**,***) starlevels(5 1 .1) summstat(N \ r2 \ r2_a) summtitle(N \ R-squared \ Adjusted R-squared) ctitle("", "(7*, Multiply imputed, party controls)") title("", "Legislative elections") merge
mibeta altlog_ae daysbeforeED_0_001 ipollse pr tk_pr enpp tk_enpp vote_ tk_vote partyold tk_partyold niche tk_niche gov_ tk_gov inc tk_inc, absorb(_couXpar_)

erase TimelinePartiesMI_Alt.dta

